package com.powergisol.gis.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.powergisol.gis.entity.TabLayer;
import com.powergisol.gis.entity.TabScene;
import com.powergisol.gis.entity.TabTree;
import com.powergisol.gis.service.TabSceneService;
import com.powergisol.gis.util.Result;
import com.powergisol.gis.vo.CopyTreeVo;
import com.powergisol.gis.vo.TabUserVo;
import com.powergisol.gis.vo.TreeSortDataVO;
import com.powergisol.gis.vo.TreeSortVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;
import java.util.List;
import java.util.Map;

/**
 * Description:场景控制器
 */
@RestController
@RequestMapping(value = "scene")
@Api(description = "场景管理" ,value = "场景管理")
public class TabSceneController extends BaseController{
    @Autowired
    private TabSceneService tabSceneService;

    @RequestMapping(value = "listScene", method = RequestMethod.POST)
    @ApiOperation(value = "场景列表", notes = "场景列表")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "sceneName", value = "场景名称", defaultValue = "0", required = false, paramType = "query"),
            @ApiImplicitParam(dataType = "Integer", name = "page", value = "页数(默认1)", defaultValue = "0", required = false, paramType = "query"),
            @ApiImplicitParam(dataType = "Integer", name = "limit", value = "每页条数(默认10)", defaultValue = "0", required = false, paramType = "query")
    })
    public Result listScene(TabScene tabScene, Principal p){
        Result result = new Result();
        TabUserVo tabUserVo = getUser(p);
        tabScene.setUserId(tabUserVo.getId());
        PageHelper.startPage(tabScene.getPage(), tabScene.getLimit());
        PageInfo<Map<String,Object>> pageInfo = new PageInfo<>(tabSceneService.listScene(tabScene));
        result.setCount((int) pageInfo.getTotal());
        result.setData(pageInfo.getList());
        return result;
    }

    @RequestMapping(value = "saveLayer", method = RequestMethod.POST)
    @ApiOperation(value = "图层保存", notes = "图层保存")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Integer", name = "sceneId", value = "场景id", defaultValue = "0", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "layerName", value = "图层名称", defaultValue = "0", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "serviceName", value = "服务名称", defaultValue = "0", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "Integer", name = "treeId", value = "树形结构父级id", defaultValue = "0", required = true, paramType = "query")
    })
    public Result saveLayer(TabLayer tabLayer){
        return tabSceneService.saveLayer(tabLayer);
    }

    @RequestMapping(value = "saveScene", method = RequestMethod.POST)
    @ApiOperation(value = "保存场景", notes = "保存场景")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "sceneName", value = "场景名称", defaultValue = "0", required = false, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "sceneDesc", value = "场景描述", defaultValue = "0", required = false, paramType = "query")
    })
    public Result saveScene(@Validated TabScene tabScene, Principal p){
        TabUserVo tabUserVo = getUser(p);
        return tabSceneService.saveScene(tabScene,tabUserVo.getId());
    }

    @RequestMapping(value = "upScene", method = RequestMethod.POST)
    @ApiOperation(value = "保存场景", notes = "保存场景")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "sceneName", value = "场景名称", defaultValue = "0", required = false, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "sceneDesc", value = "场景描述", defaultValue = "0", required = false, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "imgUrl", value = "缩略图base64", defaultValue = "0", required = false, paramType = "query"),
            @ApiImplicitParam(dataType = "Integer", name = "upSceneId/id", value = "场景id", defaultValue = "0", required = true, paramType = "query")
    })
    public Result upScene(@Validated TabScene tabScene,Principal p){
        TabUserVo tabUserVo = getUser(p);
        return tabSceneService.upScene(tabScene,tabUserVo.getId());
    }

    @RequestMapping(value = "getSceneById", method = RequestMethod.POST)
    @ApiOperation(value = "根据场景id获取场景详情", notes = "根据场景id获取场景详情")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Integer", name = "id", value = "场景id", defaultValue = "0", required = true, paramType = "query")
    })
    public Result getSceneById(Integer id){
        return tabSceneService.getSceneById(id);
    }

    @RequestMapping(value = "delScene", method = RequestMethod.POST)
    @ApiOperation(value = "场景树删除", notes = "场景树删除")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Integer", name = "id", value = "树id", defaultValue = "0", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "Integer", name = "upSceneId", value = "场景id", defaultValue = "0", required = true, paramType = "query")
    })
    public Result delScene(Integer id,Integer upSceneId,Principal p){
        TabUserVo tabUserVo = getUser(p);
        return tabSceneService.delScene(id,upSceneId,tabUserVo.getId());
    }

    @RequestMapping(value = "saveTree", method = RequestMethod.POST)
    @ApiOperation(value = "新增树菜单", notes = "新增树菜单")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "treeName", value = "名称", defaultValue = "0", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "Integer", name = "pId", value = "父id", defaultValue = "0", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "Integer", name = "upSceneId", value = "场景id", defaultValue = "0", required = true, paramType = "query")
    })
    public Result saveTree(@Validated TabTree tabTree){
        return tabSceneService.saveTree(tabTree);
    }

    @RequestMapping(value = "upTree", method = RequestMethod.POST)
    @ApiOperation(value = "修改树菜单或图层", notes = "修改树菜单或图层")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Integer", name = "id", value = "树菜单id", defaultValue = "0", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "treeName", value = "名称", defaultValue = "0", required = false, paramType = "query"),
            @ApiImplicitParam(dataType = "Integer", name = "pId", value = "父id", defaultValue = "0", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "Integer", name = "upSceneId", value = "场景id", defaultValue = "0", required = true, paramType = "query")
    })
    public Result upTree(@Validated TabTree tabTree){
        return tabSceneService.upTree(tabTree);
    }

    @RequestMapping(value = "treeScene", method = RequestMethod.POST)
    @ApiOperation(value = "场景树菜单", notes = "场景树菜单")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Integer", name = "sceneId", value = "场景id", defaultValue = "0", required = false, paramType = "query")
    })
    public Result treeScene(Integer sceneId,Principal principal){
        TabUserVo tabUserVo = getUser(principal);
        return tabSceneService.treeScene(sceneId,tabUserVo.getId());
    }

    @RequestMapping(value = "sceneDetail", method = RequestMethod.POST)
    @ApiOperation(value = "场景详情", notes = "场景详情")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Integer", name = "sceneId", value = "场景id", defaultValue = "0", required = false, paramType = "query")
    })
    public Result sceneDetail(Integer sceneId){
        return tabSceneService.sceneDetail(sceneId);
    }

    @RequestMapping(value = "copyScene", method = RequestMethod.POST)
    @ApiOperation(value = "场景复制", notes = "场景复制")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Integer", name = "isStructure", value = "是否按结构(1是0否)", defaultValue = "0", required = false, paramType = "query"),
            @ApiImplicitParam(dataType = "Integer", name = "sceneId", value = "场景id", defaultValue = "0", required = false, paramType = "query"),
            @ApiImplicitParam(dataType = "List<TabTree>", name = "treeList", value = "选择的树形菜单数组", defaultValue = "0", required = false, paramType = "query")
    })
    public Result copyScene(@RequestBody CopyTreeVo vo){
        return tabSceneService.copyScene(vo);
    }


    @RequestMapping(value = "deleteScene", method = RequestMethod.POST)
    @ApiOperation(value = "场景删除", notes = "场景删除")
    @ApiImplicitParam(dataType = "Integer", name = "sceneId", value = "场景Id", required = true)
    public Result deleteScene(Integer sceneId){
       return tabSceneService.deleteScene(sceneId);
    }

    @RequestMapping(value = "updateSort", method = RequestMethod.POST)
    @ApiOperation(value = "修改服务排序", notes = "修改服务排序")
    @ApiImplicitParam(dataType = "List<TreeSortVO> ", name = "sortVOList", value = "场景排序数组", required = true)
    public void updateSort(@RequestBody List<TreeSortVO> sortVOList){
        tabSceneService.updateSort(sortVOList);
    }

    @RequestMapping(value = "updateTreeSort", method = RequestMethod.POST)
    @ApiOperation(value = "修改树排序", notes = "修改树排序")
    @ApiImplicitParam(dataType = "TreeSortDataVO", name = "treeSortDataVO", value = "", required = true)
    public void updateTreeSort(TreeSortDataVO treeSortDataVO){
        tabSceneService.updateTreeSort(treeSortDataVO);
    }

}
